# -*- coding: ISO-8859-1 -*-

class vCardExporter:

    """
    Simple class for exporting contacts as vCard
    """

    def __init__(self):
        self._result = [
            "BEGIN:VCARD",
            "VERSION:3.0"
        ]


    def append(self, val):
        self._result.append(val)


    def vcadd(self, par, value):
        val = value[:70]
        for x in range((len(par + value)-69)/70):
            val += "\r\n " + value[70*(x+1):70*(x+1)+70]
        return par + ":" + str(val)


    def addLine(self, par, val):
        if val:
            self.append(self.vcadd(par, val))


    def vCard_Personals(self, contacts):
        "Exports personal contact data in VCARD format"
        result = self._result
        a = self.append
        a("CATEGORIES:Personal")
        al = self.addLine
        for contact in contacts:
            firstname = contact.getFirstname()
            lastname = contact.getLastname()
            if firstname and lastname:
                al("N", "%s, %s" % (lastname, firstname))
                al("FN", "%s %s" % (firstname, lastname))
            al("ORG", contact.getOrganizationName())
            al("TITLE", contact.getJobtitle())
            address = contact.getAddress()
            if address:
                al("ADR", '%s;%s %s;%s' % (address, contact.getPo(), 
                                           contact.getCity(),
                                           contact.getCountry()))
            al("TEL;TYPE=WORK", contact.getOffice_phone())
            al("EMAIL;TYPE=INTERNET", contact.getEmail())
            al("TEL;TYPE=HOME", contact.getPrivate_phone())
            al("TEL;TYPE=CELL", contact.getMobile_phone())
            al("URL", contact.getWeb())
        a("END:VCARD\r\n")
        return str(self)


    def vcard_Companies(self, contacts):
        "Exports personal contact data in VCARD format"
        result = self._result
        a = self.append
        a("CATEGORIES:Business")
        al = self.addLine
        for contact in contacts:
            al("N", contact.Title())
            address = contact.getAddress()
            if address:
                al("ADR", '%s;%s %s;%s' % (address, contact.getPo(), 
                                           contact.getCity(),
                                           contact.getCountry()))
            al("TEL;TYPE=PREF", contact.getPhone())
            al("EMAIL;TYPE=INTERNET", contact.getEmail())
            al("URL", contact.getWeb())
        a("END:VCARD\r\n")
        return str(self)


    def __str__(self):
        return '\r\n'.join(self._result)
        
        
if __name__ == '__main__':
    
    import random
    
    class Contact:
    
        """
        A simple class that returns the methodname when calling any method
        Cheap way to generate pseudotests
        """
        
        def stf(self):
            return self.st
    
        def __getattr__(self, st):
            self.st = st
            return self.stf
            
            
    print '###########################'
    exporter = vCardExporter()
    print exporter.vCard_Personals([Contact() for i in range(4)])
    print '###########################'
    exporter = vCardExporter()
    print exporter.vcard_Companies([Contact() for i in range(4)])